$(function() {
	var resoucesGrid = $("#resoucesGrid");
	var types = {
			"MENU":"菜单",
			"FUNCTION":"功能",
			"BLOCK":"区域"
	};
	resoucesGrid.treegrid({
		url : '/resources/permission/list',
		border : false,
		fit : true,
		idField : 'id',
		treeField : 'name',
		fitColumns:true,
		columns : [ [ {
			title : '名称',
			field : 'name',
			width : 180
		}, {
			field : 'permissionKey',
			title : '标识',
			align : 'center',
			width : 100
		}, {
			field : 'type',
			title : '类型',
			align : 'center',
			width : 80,
			formatter:function(val){
				return types[val];
			}
		}, {
			field : 'path',
			title : '路径',
			align : 'center',
			width : 150
		},{
			field: 'resource', 
			title: '资源',
			width: 170
		},{
			field : 'weight',
			title : '权重',
			align : 'center',
			width : 80
		},{
			field: 'description', 
			title: '描述', 
			width: 200
		}, {
			field : 'enable',
			title : '状态',
			align : 'center',
			width : 80,
			formatter:function(val){
				return val ? "<div style='color:#fff;background-color:#66CC66;border-radius:10px;width:40px;height:20px;line-height:20px;margin:0px auto'>可用</div>" : "<div style='background-color:red;border-radius:10px;width:40px;height:20px;line-height:20px;margin:0px auto'>禁用</div>";
			}
		},
	      {
	        field: 'edit', title: '操作', width: 100, align: 'center', formatter: function (val, row) {
	        var btns = [];
	        btns.push('<a data-id="' + row.id + '" class="ctr actions fa fa-pencil-square-o edit">编辑</a>');
	        btns.push('<a data-id="' + row.id + '" class="ctr actions fa fa-trash-o delete">删除</a>');
	        return btns.join("");
	      }
	      }
		
		] ],
		toolbar:[
			{
				iconCls:"icon-add",
				text:"创建权限",
				handler: function () {
					formDialog();
				}
			}
		],
		onLoadSuccess : function(){
			resoucesGrid.treegrid('collapseAll');
		}
	});
	var gridPanel = resoucesGrid.treegrid("getPanel");

	  //给操作按钮绑定事件
	  gridPanel.on("click", "a.edit", function () {
	    var id = this.dataset.id;
	    formDialog(id);
	  }).on("click", "a.delete", function () {
	    var id = this.dataset.id;
	    $.messager.confirm("提示", "是否删除？", function (r) {
	      if (r) {
	        $.get("/resources/permission/delete?id=" + id).success(function () {
	          //删除成功
	        	resoucesGrid.treegrid("reload");
	        });
	      }
	    })
	  });
	  
	  /**
	   * 表单窗口
	   */
	  function formDialog(id) {
	    var dialog = $("<div/>").dialog({
	      iconCls: 'fa fa-plus',
	      title: (id ? '编辑' : '创建') + '权限',
	      href: '/resources/permission/' + (id ? 'load?id=' + id : 'form'),
	      width: 680,
	      modal: true,
	      height: 400,
	      onClose: function () {
	        //销毁窗口
	        $(this).dialog("destroy");
	      },
	      buttons: [
	        {
	          text: '保存',
	          handler: function () {
	            var permissionForm = $("#permissionForm");
	            if (permissionForm.form("validate")) {
	              $.post("/resources/permission/" + (id ? 'update' : 'save'),
	                permissionForm.serialize()
	              ).success(function () {
	            	resoucesGrid.treegrid("reload");
	                dialog.dialog('close');
	              });
	            }
	          }
	        }
	      ]
	    });
	  }
});